/*DONE* Table [dbo].[<CompanyName>$Transfer Header$5ecfc871-5d82-43f1-9c54-59685e82318d]  2/2  Script Date: 14.1.2021 - JI ******/
/*Add prestaging table for one specific company*/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @CreateTable as VARCHAR(MAX)
DECLARE @Company as VARCHAR(100)
DECLARE @CompanyID as INT

SET @CompanyID = X --The X is the value that needs to be replaced with the [Company] ID from LSInsight$Companies
SET @Company = (SELECT LSInsight$Companies.[CompanyPrefix] from dbo.LSInsight$Companies WHERE [Company] = @CompanyID)

IF @Company IS NOT NULL
BEGIN

DECLARE @DropTable as VARCHAR(MAX)
SET @DropTable = 'DROP TABLE IF EXISTS [dbo].[' + @Company + '$Transfer Header$5ecfc871-5d82-43f1-9c54-59685e82318d]'
EXEC (@DropTable)

SET @CreateTable = 'CREATE TABLE [dbo].[' + @Company + '$Transfer Header$5ecfc871-5d82-43f1-9c54-59685e82318d](
	[timestamp] [timestamp] NOT NULL,
	[No_] [nvarchar](20) NOT NULL,
	[Store-to] [nvarchar](10) NOT NULL,
	[Store-from] [nvarchar](10) NOT NULL,
	[Customer Order ID] [nvarchar](20) NOT NULL,
	[Retail Status] [int] NOT NULL,
	[Reciving_Picking No_] [nvarchar](20) NOT NULL,
	[RTC Filtering Type] [int] NOT NULL,
	[Buyer ID] [nvarchar](50) NOT NULL,
	[Buyer Group Code] [nvarchar](10) NOT NULL,
	[Created By Source Code] [nvarchar](10) NOT NULL,
	[Transfer Type] [int] NOT NULL,
	[New Dimension Set ID] [int] NOT NULL,
	[New Shortcut Dimension 1 Code] [nvarchar](20) NOT NULL,
	[New Shortcut Dimension 2 Code] [nvarchar](20) NOT NULL,
 CONSTRAINT [' + @Company + '$Transfer Header$5ecfc871-5d82-43f1-9c54-59685e82318d$Key1] PRIMARY KEY CLUSTERED 
(
	[No_] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]'

EXEC(@CreateTable)

END